import axios from 'axios'
import Qs from 'qs'
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';
import router from '../router'
// axios.defaults.baseURL = 'https://csindex.whsangbo.com/'
axios.defaults.baseURL = 'https://wjapi.surveydiscovery.com/'
axios.defaults.withCredentials = false
axios.defaults.headers['Authorization'] = "Bearer " + localStorage.getItem("uid")+"."+localStorage.getItem("token");
axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded'
axios.interceptors.response.use(res => {
    // 判断是否需要更新token及id登
    if(res.config.url.indexOf('login/index')!==-1){
        if (res.data.code === 10000) {
            localStorage.setItem('ms_username', res.data.data.nick_name);
            localStorage.setItem('token', res.data.data.token);
            localStorage.setItem('uid', res.data.data.id);
            localStorage.setItem('group_id', res.data.data.group_id);
            axios.defaults.headers['Authorization'] = "Bearer " + localStorage.getItem("uid")+"."+localStorage.getItem("token");
        }
    }
    if(res.data.code ===301){
        console.log(router);
        router.push('/login')
    }
    return res.data
})
const AxiosPost = (url,query) => {
    return axios.post(url,Qs.stringify(query))
}
export default AxiosPost;



























export const fetchData = query => {
    return request({
        url: './table.json',
        method: 'get',
        params: query
    });
};
